package show.hkl.tools.datageneration.batchproce.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import show.hkl.tools.datageneration.batchproce.model.entity.WriterConfigLogEntity;
import show.hkl.tools.datageneration.batchproce.model.vo.WriterConfigLogVO;
import show.hkl.tools.datageneration.batchproce.service.IWriterConfigLogService;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static show.hkl.tools.datageneration.batchproce.config.WriterSchedulingConfiguration.flag;
import static show.hkl.tools.datageneration.batchproce.config.WriterSchedulingConfiguration.threadNum;

@Api(tags = "配置中心")
@RestController
@RequestMapping("/writer/config")
public class WriterConfig {

    @Resource
    private IWriterConfigLogService writerConfigService;

    private WriterConfigLogEntity entity = new WriterConfigLogEntity();
    private final static String ip = NetUtil.getLocalhost().toString();

    @ApiOperation(value = "启用操作")
    @GetMapping("/enable")
    public String enable() {
        String ip = NetUtil.getLocalhost().toString();

        entity.setIpAddress(ip);
        entity.setConfigItem("flag");
        entity.setTiemValueOld(flag ? "true" : "false");
        entity.setTiemValueNew("true");
        entity.setId(null);

        writerConfigService.save(entity);

        flag = true;

        return "启用成功";
    }

    @ApiOperation(value = "禁用操作")
    @GetMapping("/disable")
    public String disable() {

        entity.setIpAddress(ip);
        entity.setConfigItem("flag");
        entity.setTiemValueOld(!flag ? "true" : "false");
        entity.setTiemValueNew("true");
        entity.setId(null);
        writerConfigService.save(entity);

        flag = false;

        return "禁用成功";
    }


    @ApiOperation(value = "设置线程")
    @GetMapping("/threadNum")
    public String threadNum(String number) {
        Integer intNumber = Convert.toInt(number);
        if (ObjectUtil.isNull(intNumber)) {
            return "修改失败";
        } else if (intNumber < 1) {
            return "修改失败";
        } else if (intNumber > 100) {
            return "修改失败";
        } else {

            entity.setIpAddress(ip);
            entity.setConfigItem("threadNum");
            entity.setTiemValueOld(String.valueOf(threadNum));
            entity.setTiemValueNew(String.valueOf(intNumber));
            entity.setId(null);
            writerConfigService.save(entity);

            threadNum = intNumber;
            return "修改成功";
        }
    }

    @ApiOperation(value = "参数详情")
    @GetMapping("detail")
    public Map<String, Object> detail() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("flag", flag);
        result.put("threadNum", threadNum);

        return result;
    }

    @GetMapping("list")
    @ApiOperation(value = "查询列表")
    public List<WriterConfigLogVO> list() {

        return writerConfigService.lambdaQuery()
                .orderByDesc(WriterConfigLogEntity::getRowCreateTime)
                .list()
                .stream()
                .map(entity -> BeanUtil.copyProperties(entity, WriterConfigLogVO.class))
                .collect(Collectors.toList());
    }

}
